package com.blog.admin;

import com.blog.controller.BaseController;
import com.blog.kit.StringKit;
import com.blog.model.Admin;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.jfinal.validate.Validator;

import java.util.Objects;


/**
 * Created by ${wangjian} on 2017/9/5.
 */
public class AdminValidator extends Validator {

    {
        setShortCircuit(true);
    }

    @Override
    protected void validate(Controller controller) {
        String action = getActionKey();
        if (action.contains("/blog/admin/login")) {
            loginValidate(controller);
        } else if (action.contains("/blog/admin/addContent")) {
            addContentValidate(controller);
        } else if (action.contains("/blog/admin/deleteContent")) {
            validateRequiredString("logId", "error", "不能为空");
        } else if (action.contains("/blog/admin/batchDelete")) {
            validateRequiredString("List", "error", "不能为空");
        } else if (action.contains("/blog/admin/deleteImg")) {
            validateRequiredString("id", "error", "不能为空");
        }else if (action.contains("/blog/admin/batchDeleteImg")) {
            validateRequiredString("List", "error", "不能为空");
        }else if (action.contains("/blog/admin/uploadImg")) {
            UploadFile uploadFile = controller.getFile("file");
            if (null == uploadFile) {
                addError("error", "图片不能为空！");
            }
            }
        else if (action.contains("/blog/admin/addIdea")) {
            validateRequiredString("idea", "error", "不能为空");
        }else if (action.contains("/blog/admin/deleteIdea")) {
            validateRequiredString("id", "error", "不能为空");
        }else if (action.contains("/blog/admin/batchDeleteIdea")) {
            validateRequiredString("List", "error", "不能为空");
        }else if (action.contains("/blog/admin/deleteMessage")) {
            validateRequiredString("id", "error", "不能为空");
        }else if (action.contains("/blog/admin/batchDeleteMsg")) {
            validateRequiredString("List", "error", "不能为空");
        }
    }

    @Override
    protected void handleError(Controller c) {
        BaseController baseController = (BaseController) c;
        String action = getActionKey();
        if (action.contains("/blog/admin/login")) {
            baseController.setErrorCookie(c.getAttr("error").toString());
            c.redirect("/blog/admin");
        } else if (action.contains("/blog/admin/addContent")) {
            c.redirect("/blog/admin/addBlog");
        } else if (action.contains("/blog/admin/deleteContent")) {
            c.redirect("/blog/admin/content");
        } else if (action.contains("/blog/admin/batchDelete")) {
            c.redirect("/blog/admin/content");
        } else if (action.contains("/blog/admin/deleteImg")) {
            c.redirect("/blog/admin/album");
        }else if (action.contains("/blog/admin/batchDeleteImg")) {
            c.redirect("/blog/admin/album");
        }else if (action.contains("/blog/admin/uploadImg")) {
            c.redirect("/blog/admin/album");
        }else if (action.contains("/blog/admin/addIdea")) {
            c.redirect("/blog/admin/idea");
        }else if (action.contains("/blog/admin/deleteIdea")) {
            c.redirect("/blog/admin/idea");
        }else if (action.contains("/blog/admin/batchDeleteIdea")) {
            c.redirect("/blog/admin/idea");
        }else if (action.contains("/blog/admin/deleteMessage")) {
            c.redirect("/blog/admin/message");
        }else if (action.contains("/blog/admin/batchDeleteMsg")) {
            c.redirect("/blog/admin/message");
        }
    }

    private void loginValidate(Controller controller) {
        validateRequiredString("userName", "error", "用户名不能为空");
        validateRequiredString("password", "error", "密码不能为空");
        Admin admin = Admin.dao.getByName(controller.getPara("userName"));
        if (null == admin) {
            addError("error", "errorMsg");
        }
        if (!Objects.equals(admin.getPassword(), StringKit.MD5(admin.getSalt() + controller.getPara("password")))) {
            addError("error", "errorMsg");
        }
    }

    private void addContentValidate(Controller controller) {
        UploadFile uploadFile = controller.getFile("file");
        if (null == uploadFile) {
            addError("error", "图片不能为空！");
        }
        validateRequiredString("title", "error", "不能为空");
        validateRequiredString("edit", "error", "不能为空");
        validateRequiredString("digest", "error", "不能为空");
        validateRequiredString("keywords", "error", "不能为空");
        validateRequiredString("alias", "error", "不能为空");
    }

}


